#include<stdio.h>
#include<iostream>
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<pthread.h>
#include "hotel_manage.h"
#include"tools.h"

extern unsigned int day;
using namespace std;
//开房
void HotelManage::in(void)
{
	printf("请输入开房人员的姓名：");
	string name;
	cin>>name;
	cout<<"请输入身份证号："<<endl;
	uint32_t id;
	cin>>id;
	Guest* gus=new Guest(name,id);
	show();
	printf("\n");
	cout<<"请输入你选择的房间号(绿色的为可以住的，红色是已被入住的)：";
	uint16_t room_id;
	cin>>room_id;
	uint8_t floor=room_id/100;
	if(floor>hotel->get_floor())
	{
		cout<<"请输入正确的房间号"<<endl;
	}
	uint8_t room_cnt=room_id%(hotel->get_room_cnt());
	//判断该房间是否已被住满
	if(hotel->rooms[floor-1][room_cnt-1].cnt==hotel->rooms[floor-1][room_cnt-1].get_type())
	{
		cout<<"该房间已满"<<endl;
		return;
	}
	uint8_t cnt=hotel->rooms[floor-1][room_cnt-1].cnt;
	hotel->rooms[floor-1][room_cnt-1].ingus(*gus);
	
}
//构造函数
HotelManage::HotelManage(void)
{
	hotel=new Hotel;

}
//退房
void HotelManage::out(void)
{
	printf("请输入姓名：");
	char name[20];
	cin>>name;
	printf("请输入省份证：");
	uint32_t temp_id;
	cin>>temp_id;
	Guest temp_gus(name,temp_id);

	printf("请输入退房的房间号:");
	uint16_t id;
	cin>>id;
	int floor=id/100;
	int cnt=id%(hotel->get_room_cnt());
	for(int i=0;i<hotel->rooms[floor-1][cnt-1].cnt;i++)
	{
		if(hotel->rooms[floor-1][cnt-1].guests[i] == temp_gus)
		{
			hotel->rooms[floor-1][cnt-1].outgus(temp_gus,i);
			int temp_day=day-hotel->rooms[floor-1][cnt-1].get_day();
			printf("退房成功，请收取%f元房钱\n",hotel->rooms[floor-1][cnt-1].get_price()*temp_day);
			return ;
		}
	}
	printf("退房失败\n");
	
}
//查看全部房间
void HotelManage::show(void)
{
	uint8_t floor=hotel->get_floor();
	uint8_t room_cnt=hotel->get_room_cnt();

	for(int i=0;i<hotel->get_floor();i++)
	{
		if(i==0)
		{
			printf("单人间:(房价：%f)\n",hotel->rooms[i][0].get_price());
		}
		if(i==2)
		{
			printf("\n双人间:(房价：%f)\n",hotel->rooms[i][0].get_price());
		}
		if(i==4)
		{
			printf("\n三人间:(房价：%f)\n",hotel->rooms[i][0].get_price());
		}
		if(i==6)
		{
			printf("\n四人间:(房价：%f)\n",hotel->rooms[i][0].get_price());
		}
		for(int j=0;j<hotel->get_room_cnt();j++)
		{
			if(hotel->rooms[i][j].cnt==hotel->rooms[i][j].get_type())
			{
				printf("\033[1;31m %d \033[0m",hotel->rooms[i][j].get_id());
			}
			else
			{
				printf("\033[1;36m %d \033[0m",hotel->rooms[i][j].get_id());
			}
			
		}
	}
}
//查询
void HotelManage::query(void)
{
	printf("请输入要查询的姓名：");
	string name;
	cin>>name;
	printf("请输入身份证号：");
	uint32_t temp_id;
	cin>>temp_id;
	Guest temp_gus(name,temp_id);
	for(int i=0;i<hotel->get_floor();i++)
	{
		for(int j=0;j<hotel->get_room_cnt();j++)
		{
			for(int k=0;k<hotel->rooms[i][j].cnt;k++)
			{
				if(hotel->rooms[i][j].guests[k]==temp_gus)
				{
					printf("你的房间号：%d\n",hotel->rooms[i][j].get_id());
					return ;
				}
			}
		}
	}
	printf("查无此人\n");
}
//界面
void menu(void)
{
	printf("1、开房\n");
	printf("2、退房\n");
	printf("3、查询\n");
	printf("4、关机\n");
}
//线程执行闹钟
void* alarm_pth(void* temp)
{
	while(1)
	{
		alarm(10);
		sleep(10);
	}
}
//信号处理
void add_day(int sig)
{
	day++;
}
//运行
void HotelManage::run(void)
{
	pthread_t pid;
	pthread_create(&pid,NULL,alarm_pth,NULL);
	signal(SIGALRM,add_day);
	while(true)
	{
		menu();
		int n=0;
		cin>>n;
		if(1==n)
		{
			in();
		}
		if(2==n)
		{
			out();
		}
		if(3==n)
		{
			query();
		}
		if(4==n)
		{
			return;
		}
	}
}

